from collections import Counter

#画想要的曲线
import matplotlib
import numpy as np
import pandas as pd
import datetime

from matplotlib import pyplot as plt

df = pd.read_csv('../画图/DMSC.csv')

# 把电影区分开
ans = [pd.DataFrame(y) for x, y in df.groupby('Movie_Name_EN', as_index=False)]
#获得电影名
listOfNames = df['Movie_Name_EN'].unique()
#获得要使用的indexes
indexes = [1,2,5,6,16,17,18,20,21,22,23,25,28,29]

#先获得所有数据的distance信息
data_distance_dfs = []
#先处理电影数据
#获得每一个值的临界数，并且添加一行
def getDistanceOfEachMovie():
    for df in ans:
        df.dropna(subset=['Date'])
        max_date = df['Date'].value_counts().idxmax()
        df['distance'] =  df['Date'].apply(getDistance,args=(max_date,))

def getDistance(target,max_date):
    date1=datetime.datetime.strptime(target[:],"%Y-%m-%d")
    date2=datetime.datetime.strptime(max_date[:],"%Y-%m-%d")
    return (date1 - date2).days

getDistanceOfEachMovie()

#再处理五四晚会和央视春晚数据
party_df = pd.read_csv('../画图/副本2021五四晚会微博.csv')
spring_festival_party_df = pd.read_csv('../画图/chunwan.csv')
partyDfs = [party_df,spring_festival_party_df]

def getDistanceOfEachParty():
    for df in partyDfs:
        df.dropna(subset=['date'])
        max_date = df['date'].value_counts().idxmax()
        df['distance'] = df['date'].apply(getDistanceOfPartyData, args=(max_date,))
        ans.append(df)

def getDistanceOfPartyData(target, max_date):
    date1 = datetime.datetime.strptime(target[:], "%Y/%m/%d")
    date2 = datetime.datetime.strptime(max_date[:], "%Y/%m/%d")
    return (date1 - date2).days

getDistanceOfEachParty()

# listOfNames.append('May Fourth Party')
# listOfNames.append('Spring Festival Gala')
listOfNames = np.append(listOfNames, 'May Fourth Party')
listOfNames = np.append(listOfNames,'Spring Festival Gala')

#获得所有的频度和distance信息
def getMoviceDistanceDfs():
    for df in ans:
        frequency = df['distance'].value_counts()
        size = len(df)
        movie_distance_df = pd.DataFrame({'distance':frequency.index, 'frequency':frequency.values})
        #只保留-30到30的distance的值
        movie_distance_df = movie_distance_df.loc[(movie_distance_df['distance'] >= -30) & (movie_distance_df['distance'] <= 30)]
        data_distance_dfs.append(movie_distance_df)

getMoviceDistanceDfs()

#然后把所有的频度合并，得到平均的频度信息
def getAvgDistanceDf():
    result = Counter({})
    sum = 0
    for i,df in enumerate(ans):
        if i not in indexes:
            continue;
        sum += len(df)
        frequency = df['distance'].value_counts()
        movie_distance_df = pd.DataFrame({'distance':frequency.index, 'frequency':frequency.values})
        mydict = dict(zip(movie_distance_df.distance, movie_distance_df.frequency))
        result += Counter(mydict)
    df = pd.DataFrame(list(result.items()),columns = ['distance','frequency'])
    df['frequency'] = df['frequency'].apply(lambda x: x/sum)
    return df

result = getAvgDistanceDf()
result = result.sort_values(by='distance')
result.to_csv('所有数据的平均值信息.csv')

#然后对于每一个df 逐一处理成 distance和频率的信息
#要得到这个里面的distance和频度的信息
def getFrequencyOfDf():
    for idx,df in enumerate(data_distance_dfs):
        size = len(ans[idx])
        df['frequency'] = df['frequency'].apply(lambda x: x/size)

getFrequencyOfDf()
#画拟合曲线要出的图
#画拟合之后的曲线
def exp_model(x,a,b,d):
    return a*np.exp(-b*(abs(x)))+d
def m_model(x,a,b,c):
    return a*pow(x,b)+c

x1 = list(range(-30,1))
x2 = list(range(1,31))
a = 0.08826636201514965
b = 0.5187754161145138
d = -8.955432916439745e-06
y1 = [exp_model(i,a,b,d) for i in x1]
a1 = -2.41327632e+02
b1 = 7.98216083e-05
d1 = 2.41392257e+02
y2 = [m_model(i,a1,b1,d1) for i in x2]
#画总图
#画想要的曲线
colors = [plt.cm.tab10(i/float(len(listOfNames)-1)) for i in range(len(listOfNames))]
# Draw Plot for Each Movie
plt.figure(figsize=(16, 10), dpi= 80, facecolor='w', edgecolor='k')
#绘图
for i, df in enumerate(data_distance_dfs):
    if i not in indexes:
        continue;
    plt.scatter('distance', 'frequency', data=df,cmap=colors[i],s=20,label=listOfNames[i])

plt.plot(x1,y1, "--", color="#F51E1F", linewidth='3', label='$a*e^{-b*x} + d$') #red
plt.plot(x2,y2, "--", color="#3952B2", linewidth='3',label='$a*x^b + d$')

# Decorations
plt.gca().set(xlim=(-30,30), ylim=(0, 0.125),xlabel='distance', ylabel='frequency')
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.title("The frequency of movie comment", fontsize=22)
plt.legend(fontsize=12)
plt.savefig('result.png')
plt.show()